package org.easy.amapiplocation;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.easy.core.util.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * https://lbs.amap.com/api/webservice/guide/api/weatherinfo
 */

public class AmapIpClient {


    /**
     * 高德地图请求地址
     */
    private final static String REQUEST_URL_TEMPLATE = "https://restapi.amap.com/v3/ip";


    /**
     * 实况天气
     *
     * @param key    请求服务权限标识 https://console.amap.com/dev/key/app 查看key
       @Param ip
     * @return
     */
    public IpAddress getAddress(String key, String ip) {
        Map<String, String> params = new HashMap<>();
        params.put("key", key);
        //lng经度在前，lat纬度在后
        params.put("ip", ip);

        String response = HttpUtil.sendGet(REQUEST_URL_TEMPLATE, null, params);
        JSONObject jsonObject = JSON.parseObject(response);
        assert jsonObject != null;
        return JSON.parseObject(jsonObject.toJSONString(), IpAddress.class);
    }


}